const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const simpleFormat = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}

const setDeadline = days => {
  const date = new Date()
  date.setDate(new Date().getDate() + days)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const paramEncode = (param, except = []) => {
  var keys = Object.keys(param).sort()
  var strArr = []
  except.push('_nav_')
  keys.forEach(k => {
    if (except.indexOf(k) == -1) {
      strArr.push(`${k}=${param[k]}`)
    }
  })
  return strArr.length > 0 ? '?' + strArr.join('&') : ''
}

// 验证是否是手机号码
function vailPhone(number) {
  let flag = false
  let myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/
  if (number.length != 11) {
    flag = flag
  } else if (!myreg.test(number)) {
    flag = flag
  } else {
    flag = true
  }
  return flag
}

export default {
  formatTime: formatTime,
  simpleFormat: simpleFormat,
  setDeadline: setDeadline,
  paramEncode: paramEncode,
  vailPhone: vailPhone
}
